﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Core;
using Microsoft.Vbe.Interop;
using SQLitePCL;

namespace 枸杞VBE插件
{
    internal static class ExtensionMethod
    {
        internal static CommandBarButton AddButton(this CommandBarControls Controls, string 名称, int 图标ID, _CommandBarButtonEvents_ClickEventHandler 委托)
        {
            CommandBarButton 按钮 = (CommandBarButton)Controls.Add(MsoControlType.msoControlButton);
            按钮.Caption = 名称;
            按钮.FaceId = 图标ID;
            按钮.Style = MsoButtonStyle.msoButtonIconAndCaption;
            //按钮.Tag = "控件" + VBE助手实例.标签号++;
            ////按钮.Picture = "";
            按钮.Click += 委托;
            //VBE助手实例.控件字典[关键字 ?? 名称] = 按钮;
            return 按钮;
        }

        internal static CommandBarButton AddButton(this CommandBarControls Controls, string 名称, int 图标ID, Action 事件)
        {
            return Controls.AddButton(名称, 图标ID, new _CommandBarButtonEvents_ClickEventHandler((CommandBarButton _, ref bool _) => 事件.Invoke()));
        }

        internal static CommandBarButton AddButton(this CommandBar commandBar, string 名称, int 图标ID, _CommandBarButtonEvents_ClickEventHandler 委托)
        {
            return commandBar.Controls.AddButton(名称, 图标ID, 委托);
        }

        internal static CommandBarButton AddButton(this CommandBar commandBar, string 名称, int 图标ID, Action 事件)
        {
            return commandBar.Controls.AddButton(名称, 图标ID, 事件);
        }

        internal static CommandBarPopup 添加菜单(this CommandBarControls Controls, string 名称, string 关键字 = null)
        {
            CommandBarPopup 选项菜单 = (CommandBarPopup)Controls.Add(MsoControlType.msoControlPopup);
            //选项菜单.Caption = 名称;
            //if (关键字 != null) VBE助手实例.控件字典[关键字] = 选项菜单;
            ////Log.Info("添加菜单:{0}", 字典key ?? 名称);
            return 选项菜单;
        }

        internal static void 删除空行(this CodeModule 代码模块)
        {
            for (int i = 代码模块.CountOfLines; i > 0; i--)
            {
                if (代码模块.Lines[i, 1].Trim().Length == 0) 代码模块.DeleteLines(i, 1);
            }
        }

        internal static void 删除空行(this CodePane 代码窗格)
        {
            代码窗格.CodeModule.删除空行();
        }

        internal static void 格式化(this CodePane 代码窗格)
        {
            //  代码窗格.CodeModule.删除空行();
        }
    }
}